package com.wang.common.domain;

import lombok.Data;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

@Data
public class DelayedUser implements Delayed {
    public DelayedUser() {

    }

    public DelayedUser(int id, String name, long delayTime) {
        this.id = id;
        this.name = name;
        this.executeTime = TimeUnit.NANOSECONDS.convert(delayTime, TimeUnit.MILLISECONDS) + System.nanoTime();
    }

    private int id;
    private String name;
    private long executeTime;


    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(this.executeTime - System.nanoTime(), TimeUnit.NANOSECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        DelayedUser delayedUser = (DelayedUser) o;
        return this.getExecuteTime() > delayedUser.getExecuteTime() ? 1 : this.id < delayedUser.getExecuteTime() ? -1 : 0;
    }
}
